package com.store.app.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.utils.ToastUtils;


/**
 * on 2017/3/14 0014 14:58
 * 商品规格换算公式
 */

public class SetGoodTypeDialog extends Dialog {
    private onConfirmListener listener;
    private Activity mActivity;
    private TextView btn_cancle;
    private TextView btn_confirm;
    private TextView txt_type_cgdw;//采购单位类型
    private TextView txt_type_xsdw;//销售单位类型
    private TextView txt_type_zxbz;//最小包装类型
    private TextView txt_type_kcdw;//库存单位类型

    private TextView txt_num_cgdw;//采购单位数量
    private EditText edit_num_xsdw;//销售单位数量
    private EditText edit_num_zxbz;//最小包装数量
    private EditText edit_num_kcdw;//库存单位数量

    public SetGoodTypeDialog(Activity activity, onConfirmListener l) {
        super(activity, R.style.dialog_with_alpha);
        this.mActivity = activity;
        this.listener = l;
    }
    //各种单位设置
    public void setTypeText(String cgdw_type,String xsdw_type,String zxbz_type,String kcdw_type){
        txt_type_cgdw.setText(cgdw_type);
        txt_type_xsdw.setText(xsdw_type);
        txt_type_zxbz.setText(zxbz_type);
        txt_type_kcdw.setText(kcdw_type);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_setgoodtype);
        DisplayMetrics metric = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = (int) (metric.widthPixels) - 100;
        Window window = getWindow();
        window.setAttributes(p);
        window.setGravity(Gravity.CENTER);
        setCancelable(true);
        this.setCanceledOnTouchOutside(true);
        txt_type_cgdw = (TextView) findViewById(R.id.diasetgoodtype_tv5);
        txt_type_xsdw = (TextView) findViewById(R.id.diasetgoodtype_tv6);
        txt_type_zxbz = (TextView) findViewById(R.id.diasetgoodtype_tv7);
        txt_type_kcdw = (TextView) findViewById(R.id.diasetgoodtype_tv8);
        txt_num_cgdw = (TextView) findViewById(R.id.diasetgoodtype_tv9);//采购单位数量
        edit_num_xsdw = (EditText) findViewById(R.id.diasetgoodtype_tv10);//销售单位数量
        edit_num_zxbz= (EditText) findViewById(R.id.diasetgoodtype_tv11);//最小包装数量
        edit_num_kcdw= (EditText) findViewById(R.id.diasetgoodtype_tv12);//库存单位数量
        btn_cancle = (TextView) findViewById(R.id.tv_cancle_submit);
        btn_confirm = (TextView) findViewById(R.id.tv_ok_submit);

        txt_num_cgdw.setText("1");

        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String xsdwNum = edit_num_xsdw.getText().toString().trim();
                String zxbzNum = edit_num_zxbz.getText().toString().trim();
                if("0".equals(xsdwNum)){
                    ToastUtils.showTextToast(mActivity,"销售单位不能为0");
                    return;
                }
                if("0".equals(zxbzNum)){
                    ToastUtils.showTextToast(mActivity,"最小包装不能为0");
                    return;
                }

                if(TextUtils.isEmpty(xsdwNum)){
                    edit_num_xsdw.setText("1");
                    xsdwNum = edit_num_xsdw.getText().toString().trim();
                }
                if(TextUtils.isEmpty(zxbzNum)){
                    edit_num_zxbz.setText("1");
                    zxbzNum = edit_num_zxbz.getText().toString().trim();
                }

                if(listener != null){
                    listener.onConfirm("1",xsdwNum,zxbzNum);
                }

                dismiss();

            }
        });

        edit_num_xsdw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                edit_num_kcdw.setText(s.toString());
            }
        });
    }


    public interface onConfirmListener {
       void onConfirm(String cgdwNum, String xsdwNum, String zxbzNum);
    }
}
